const app = getApp()
var CusBase64 = require('../../utils/base64.js');

Page({
  data: {
    dateStart: '',
    dateEnd: '',
    title: '',
    detail: '',
    id:'',
    kind:'',
    orderId:'',
    userList:[],
  },
  onLoad: function (e) {
    var that = this
    that.setData({
      kind: e.kind,
      id:e.id,
      orderId:e.orderId
    })
    wx.request({
      url: getApp().data.service + '/menu/getMenuDetail',
      data: {
        id: e.id,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          title: CusBase64.CusBASE64.decoder(CusBase64.CusBASE64.decoder(res.data.data.title)),
          detail: CusBase64.CusBASE64.decoder(CusBase64.CusBASE64.decoder(res.data.data.desc)),
          dateStart:formatTime(res.data.data.startTime,"Y-M-D h:m"),
          dateEnd: formatTime(res.data.data.endTime, "Y-M-D h:m"),
          id: res.data.data.id,
          userList: changeRealName(res.data.data.userList)
        })
      }
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.title,
      path: 'pages/index/index?id='+this.data.id
    }
  },

  cancelAction: function(){
    wx.request({
      url: getApp().data.service + '/menu/cancelOrder ',
      data: {
        id: this.data.orderId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.showToast({
          title: '取消成功',
          icon:'none',
          complete: function () {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    })
  }
})

function formatTime(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function changeRealName(arr) {
  if (Object.prototype.toString.call(arr) === '[object Array]') {
    for (var i = 0, len = arr.length; i < len; i++) {
      arr[i].realName = CusBase64.CusBASE64.decoder(arr[i].realName)
    }
  }
  return (arr)
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
